package x.ovo.wechat.bot.http.request.core;

import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.json.JSONUtil;
import x.ovo.wechat.bot.common.Constant;
import x.ovo.wechat.bot.common.http.request.ApiRequest;
import x.ovo.wechat.bot.common.http.session.LoginSession;
import x.ovo.wechat.bot.http.response.InitResponse;

import java.util.Map;
import java.util.function.Function;

public class InitRequest extends ApiRequest<InitResponse> {
    public InitRequest(LoginSession session) {
        super(session);
    }

    @Override
    public Function<String, InitResponse> responseHandler() {
        return s -> JSONUtil.toBean(s, InitResponse.class);
    }

    @Override
    public String getMethod() {
        return "POST";
    }

    @Override
    public boolean isJson() {
        return true;
    }

    @Override
    public String getUrl() {
        return UrlBuilder.of(this.session.getUrl())
                .addPath(Constant.WXINIT)
                .addQuery("pass_ticket", this.session.getPassTicket())
                .addQuery("r", -System.currentTimeMillis() / 1579)
                .build();
    }

    @Override
    public Map<String, Object> getParameters() {
        super.addParameter("BaseRequest", this.baseRequest);
        return super.getParameters();
    }
}
